package com.xf.entity;

import com.xf.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author suer
 * 返回值统一格式
 */
@Data
@ApiModel(value = "AjaxResult" , description = "返回值统一格式")
public class AjaxResult<T> implements Serializable {
    private static final long serialVersionUID = -5757933746700705620L;

    @ApiModelProperty(value = "返回数据类型")
    private T result;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回响应码")
    private String code;

    @ApiModelProperty(value = "返回的结果集")
    private String cause;

    public AjaxResult() {
    }

    public AjaxResult(T result) {
        this.result = result;
    }

    public AjaxResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public AjaxResult<T> result(T result) {
        this.result = result;
        return this;
    }

    public AjaxResult<T> success() {
        this.code = ResultCodeEnum.SUCCESS.getCode();
        this.message = ResultCodeEnum.SUCCESS.getMessage();
        return this;
    }

    public AjaxResult<T> fail() {
        this.code = ResultCodeEnum.ERROR_UNKNOW.getCode();
        this.message = ResultCodeEnum.ERROR_UNKNOW.getMessage();
        return this;
    }

    public AjaxResult<T> fail(String message) {
        this.code = ResultCodeEnum.ERROR_UNKNOW.getCode();
        this.message = message;
        return this;
    }

    public AjaxResult<T> fail(String code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public AjaxResult<T> cause(String cause) {
        this.cause = cause;
        return this;
    }
}
